<?php
class it_menu_access_model extends MY_Model {
	var $model = 'it_menu_access';
	
	function GetJSON()
	{
		$ls_sql	= "SELECT	tbl_menu.menu_id,menu_label,menu_level,parent_menu_id,url,icon,
                            (CASE WHEN it_menu_access.role_id IS NULL THEN '0' ELSE '1' END) visible,
                            NVL(it_menu_access.cr,'0') \"c\", '1' \"r\",
                            NVL(it_menu_access.up,'0') \"u\", NVL(it_menu_access.dl,'0') \"d\",
                            NVL(it_menu_access.pr,'0') \"pr\", NVL(it_menu_access.dw,'0') \"dw\"
                    FROM
                            (SELECT (
                                                NVL( LPAD(e.order_num,10,0) || RPAD(e.menu_label,100,'#'), '') ||
                                                NVL( LPAD(d.order_num,10,0) || RPAD(d.menu_label,100,'#'), '') ||
                                                NVL( LPAD(c.order_num,10,0) || RPAD(c.menu_label,100,'#'), '') ||
                                                NVL( LPAD(b.order_num,10,0) || RPAD(b.menu_label,100,'#'), '') ||
                                                NVL( LPAD(a.order_num,10,0) || RPAD(a.menu_label,100,'#'), '')
                                            ) AS tree_menu, 
                                            (CASE NVL(e.menu_label, '_zz') WHEN '_zz' THEN 0 ELSE 1 END)  + (CASE NVL(d.menu_label, '_zz') WHEN '_zz' THEN 0 ELSE 1 END)  + (CASE NVL(c.menu_label, '_zz') WHEN '_zz' THEN 0 ELSE 1 END) + (CASE NVL(b.menu_label, '_zz') WHEN '_zz' THEN 0 ELSE 1 END) + (CASE NVL(a.menu_label, '_zz') WHEN '_zz' THEN 0 ELSE 1 END) AS menu_level,  
                                            a.menu_label, a.menu_id, a.parent_menu_id, a.url, a.icon
                            FROM    (SELECT * FROM it_menu WHERE app_id = '".APP."') a LEFT OUTER JOIN
                                    (SELECT * FROM it_menu WHERE app_id = '".APP."') b ON a.parent_menu_id = b.menu_id LEFT OUTER JOIN
                                    (SELECT * FROM it_menu WHERE app_id = '".APP."') c ON b.parent_menu_id = c.menu_id LEFT OUTER JOIN
                                    (SELECT * FROM it_menu WHERE app_id = '".APP."') d ON c.parent_menu_id = d.menu_id LEFT OUTER JOIN
                                    (SELECT * FROM it_menu WHERE app_id = '".APP."') e ON d.parent_menu_id = e.menu_id 
                    ) tbl_menu
                    LEFT OUTER JOIN it_menu_access ON tbl_menu.menu_id = it_menu_access.menu_id AND it_menu_access.role_id='".post('role_id')."'
                    WHERE    (tbl_menu.menu_id LIKE '%".post('keyword')."%' 
                            OR UPPER(menu_label) LIKE '%".strtoupper(post('keyword'))."%' ) 
                    ORDER BY tree_menu";
					
		return json_encode( $this->paging($ls_sql) );
	}
		
	/*function Add($arr_data){
		$this->db->insert('sw_jenis_umk', $arr_data);
	}
	
	function Update($arr_data, $arr_trans){
		$this->db->update('sw_jenis_umk', $arr_data, $arr_trans);
	}
	
	function GetSingle($as_code){
		$result = $this->db->get_where('sw_jenis_umk', array('code' => $as_code));
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}
	
	function GetSingleView($as_code)
	{
		$result = $this->db->get_where('sw_jenis_umk', array('code' => $as_code));
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}

	function DeleteSingle($as_code)
	{
		try {
			$this->db->delete('sw_jenis_umk', array('code' => $as_code));
			return '';
		} catch(Exception $e) {
			return $e->getMessage();
		}
	}*/
}
?>